import React, { Component } from "react";
import PropTypes from "prop-types";
import "./index.css";

export default class Footer extends Component {
  static propTypes = {
    todos: PropTypes.array.isRequired,
    checkAllTodo: PropTypes.func.isRequired,
    clearAllDone: PropTypes.func.isRequired,
  };
  handleCheck = (e) => {
    console.log("e.target.ischecked", e.target.checked);
    this.props.checkAllTodo(e.target.checked);
  };
  handleClearAllDone = () => {
    this.props.clearAllDone()
  }
  render() {
    const { todos } = this.props;
    const doneCount = todos.reduce((prev, curr) => {
      return (prev += curr.isDone ? 1 : 0);
    }, 0);
    let ischecked =
      doneCount === todos.length && todos.length !== 0 ? true : false;
    return (
      <div className="todo-footer">
        <label>
          <input
            type="checkbox"
            checked={ischecked}
            onChange={this.handleCheck}
          />
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{todos.length}
        </span>
        <button className="btn btn-danger" onClick={this.handleClearAllDone}>清除已完成任务</button>
      </div>
    );
  }
}
